package com.bjtu.literatureclient.shiro;

import com.bjtu.literatureclient.domain.user.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class SystemLogoutFilter extends LogoutFilter {
    private static final Logger logger = LoggerFactory.getLogger(SystemLogoutFilter.class);
	@Override  
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        //在这里执行退出系统前需要清空的数据  
        Subject subject=getSubject(request, response);
        User user = (User)subject.getPrincipal();
        logger.info("用户名：" + user.getUserName() +" 退出登录...");
        String redirectUrl=getRedirectUrl(request,response,subject);  
        ServletContext context= request.getServletContext();
        try {  
        	SecurityUtils.getSubject().logout();
            subject.logout();  
            context.removeAttribute("error");  
        }catch (SessionException e){  
            e.printStackTrace();  
        }  
        issueRedirect(request,response,"/index.html");
        return false;  
    }

}
